//
//  SpecialDetailViewController.m
//  味·道
//
//  Created by lizi on 15/7/1.
//  Copyright (c) 2015年 WMR. All rights reserved.
//

#import "SpecialDetailViewController.h"
#import "SpecialDetailTableViewCell.h"
#import "SpecialDetailTableViewCell2.h"
#import "SpecialThirdViewController.h"
#import "SpecialDetailModel.h"
#import "MaocHeader.h"

@interface SpecialDetailViewController ()
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *dataArr;//存放下面cell的数据
@end

@implementation SpecialDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.2 blue:0.5 alpha:1];
    self.dataArr = [[NSMutableArray alloc] initWithCapacity:20];
    [self WL_setTitle];
    [self createBackBtn];
    [self requestData];
    
    // Do any additional setup after loading the view.
}
- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, SCREEN_WIDTH, SCREEN_HEIGHT-55) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SpecialDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    [_tableView release];
}
- (void)WL_setTitle{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(170, 20, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"列表"];
    customLab.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:customLab];
    [customLab release];
}
- (void)createBackBtn{
    UIButton *back = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [back setTitle:@"返回" forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backFor:) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(10, 10, 50, 50);
    [self.view addSubview:back];
}
- (void)backFor:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)requestData{
    RequestManager *manager = [[RequestManager alloc] initWithRequestURL:[NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.topic.detail.php?id=%@",self.Id] requestModel:GET requestParameter:nil];
    manager.delegate = self;
    [manager startRequest];
    [manager release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#warning  网络请求 
//请求完毕后让代理对象执行的方法，同时把请求完整的data传给代理对象
- (void)requestDidFinishWithData:(NSData *)data{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSArray *list = dic[@"tlist"];
    for (NSDictionary *diction in list) {
        NSArray *list2 = diction[@"list"];
        for (NSDictionary *diction2 in list2) {
            SpecialDetailModel *detailModel = [[SpecialDetailModel alloc] init];
            [detailModel setValuesForKeysWithDictionary:diction2];
            [self.dataArr addObject:detailModel];
            [detailModel release];
        }
    }
    [self.tableView reloadData];
    [self createTableView];
}
//请求失败后把错误原因传值给代理对象
- (void)requestError:(NSError *)error{
    NSLog(@"error:%@",error);
}
#warning tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 1;
    }else{
        return self.dataArr.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return [SpecialDetailTableViewCell heightForSpecial:self.model];
    }else{
        return 90;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"cell2";
//    static NSString *tag = @"cell";
    SpecialDetailModel *detailModel = _dataArr[indexPath.row];
    if (indexPath.section == 0) {
//        SpecialDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tag];
//        if (cell == nil) {
//            cell = [[SpecialDetailTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:tag];
//        }
        SpecialDetailTableViewCell *cell = [[SpecialDetailTableViewCell alloc] init];
        [cell setModel:self.model];
        return cell;
    }else{
        SpecialDetailTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:str];
            if (cell2 == nil) {
                cell2 = [[SpecialDetailTableViewCell2 alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:str];
            }
        [cell2 setDetailModel:detailModel];
        return cell2;
    } 
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SpecialThirdViewController *thirdVC = [[SpecialThirdViewController alloc] init];
    SpecialDetailModel *detailModel = _dataArr[indexPath.row];
    thirdVC.detailModel = detailModel;
    thirdVC.Id = detailModel.ID;
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];
}
@end
